package com.dexilaonian.adpter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dexilaonian.R;
import com.dexilaonian.activity.StoreDetailActivity;
import com.dexilaonian.bean.ContentBean;
import com.dexilaonian.bean.StoreBean;
import com.dexilaonian.fragment.StoreFragment;
import com.dexilaonian.utils.ImageLoader;
import com.dexilaonian.utils.Logutil;

import java.util.List;

/**
 * Created by Administrator on 2018/5/2.
 */

public class SearchAdapter extends BaseAdapter{
    private final Context mContext;
    private final List<ContentBean> mTitleList;
    public SearchAdapter(Context context, List<ContentBean> titleList) {
        mContext = context;
        mTitleList = titleList;
    }
    @Override
    public int getCount() {
        if (mTitleList.size() == 0) {
            return 0;
        } else {
            return mTitleList.size();
        }
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewhoder = null;
        if (convertView == null) {
            viewhoder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.search, null);
            viewhoder.rlItem_search = (RelativeLayout) convertView.findViewById(R.id.rl_item_search);
            viewhoder.search_title = (TextView) convertView.findViewById(R.id.tv_search_title);
            viewhoder.search_content = (TextView) convertView.findViewById(R.id.tv_search_content);
            viewhoder.search_iv = (ImageView) convertView.findViewById(R.id.item_search_iv);
            convertView.setTag(viewhoder);
        } else {
            viewhoder = (ViewHolder) convertView.getTag();
        }
        String name = mTitleList.get(position).getName();
        final int search_id = mTitleList.get(position).getId();
        String original_price = mTitleList.get(position).getOriginal_price();
        String pic_url = mTitleList.get(position).getPic_url();
        if (!TextUtils.isEmpty(name)) {
            viewhoder.search_title.setText(name);
        }
        if (!TextUtils.isEmpty(original_price)) {
            viewhoder.search_content.setText(original_price);
        }
        ImageLoader.display(mContext,pic_url,viewhoder.search_iv);
        viewhoder.rlItem_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, StoreDetailActivity.class);
                intent.putExtra("content_id", search_id);
                mContext.startActivity(intent);
            }
        });
        return convertView;
    }
    class ViewHolder{
        TextView search_title;
        TextView search_content;
        ImageView search_iv;
        RelativeLayout rlItem_search;
    }
}
